package com.example.demo.controller;

import com.example.demo.DTO.userDTO;
import com.example.demo.Response;
import com.example.demo.dao.user;
import com.example.demo.service.userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@CrossOrigin(origins = "*")
@RestController
public class userController {

    @Autowired
    private userService userService;
    @GetMapping("/user/{id}")
    public Response<userDTO> getUserById(@PathVariable long id){
        return Response.newSuccess(userService.getUserById(id));
    }

    @DeleteMapping("/del/{id}")
    public void deleteTestById(@PathVariable long id){
        userService.deleteUserById(id);
    }


    @PutMapping("/updeamil/{id}")
    public Response<userDTO> updateUserEmailById(@PathVariable long id,
                                                @RequestParam(required = false) String email){
        return Response.newSuccess(userService.updateUserEmailById(id, email));
    }

    @PostMapping("/add")//用户注册
    public user addNewUser(@RequestBody userDTO userDTO) {
        return userService.addNewUser(userDTO);
    }

    @PostMapping("/login")//登录
    public user login(@RequestBody user user){
        return userService.getUserByNameAndPass(user.getName(),user.getPass());
    }
    @PostMapping("/user")//根据用户名查询用户信息
    public user getUserByName(@RequestBody user user){
        return userService.getUserByName(user.getName());
    }
    @PostMapping("/userState")//根据状态查询未审核用户
    public List<user> getUserByState(@RequestBody user user){
        System.out.println(user.getUserState());
        return userService.getUserByState(user.getUserState());
    }
    @PostMapping("/updState")//根据id更新用户状态
    public user updateStateById(@RequestBody user user){
        return userService.updateStateById(user.getId());
    }
    @PostMapping("/updMoney")//根据id更新用户状态
    public user updateMoneyByName(@RequestBody user user){
        return userService.updateMoneyByName(user.getName(),user.getMoney());
    }

}
